package com.study.d015propertybind.fx;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.property.ReadOnlyDoubleWrapper;
import javafx.beans.property.ReadOnlyProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
*@description 属性绑定 之 SimpleStringProperty 的使用
*@author xtayhame
*date 2020年3月4日
*version
*since v1.0
*/
public class SimpleStringPropertyMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		Data data = new Data("张三", 19);

		data.getAgeProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				System.out.println("age-old:"+oldValue + "   age-new:"+newValue);
			}
		});
		data.getNameProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				System.out.println("age-old:"+oldValue + "   age-new:"+newValue);
			}
		});

		data.setName("李四");
		data.setAge(30);

		stage.setScene(new Scene(new AnchorPane(), 300, 400));
		stage.show();



		//使用Data2  , 想使用监听的时候就可以使用，不想使用也可以
		Data2 d2 = new Data2("李逵");
		d2.getNameProperty().addListener(new ChangeListener<String>() {

			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				SimpleStringProperty sp=(SimpleStringProperty)observable;
				System.out.println(sp.getBean());
				System.out.println(sp.getName());
				System.out.println("Data2-- old="+oldValue + "    ,new="+newValue);
			}
		});
		d2.setName("编号");

		/*Button b= new Button();
			b.prefHeight(width)
		 */

		//返回只读类型的监听
		/* ReadOnlyDoubleWrapper rodw = new ReadOnlyDoubleWrapper(this, "");
		 ReadOnlyDoubleProperty rodp=rodw.getReadOnlyProperty();
		 rodp.addListener(new ChangeListener<Number>() {

			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				// TODO Auto-generated method stub

			}

		});
*/
	}



}

class Data{
	private ReadOnlyDoubleWrapper rodw = new ReadOnlyDoubleWrapper(this, "");
	private ReadOnlyDoubleProperty rodp=null;

	private SimpleStringProperty name=new SimpleStringProperty();
	private SimpleIntegerProperty age = new SimpleIntegerProperty();

	public Data(String name, int age) {
		this.name.set(name);
		this.age.set(age);
	}

	public SimpleIntegerProperty getAgeProperty() {
		return age;
	}
	public SimpleStringProperty getNameProperty() {
		return name;
	}
	public void setName(String name) {
		this.name.set(name);
	}
	public void setAge(int age) {
		this.age.set(age);
	}

}


class Data2{
	private String name;
	private SimpleStringProperty nameProperty;

	public Data2(String name) {
		if(nameProperty ==null){
			this.name = name;
		}else{
			this.nameProperty.set(name);
		}
	}

	public String getName() {
		if(this.nameProperty==null){
			return this.name;
		}
		return this.nameProperty.get();
	}

	public SimpleStringProperty getNameProperty() {
		if(nameProperty ==null){
			nameProperty = new SimpleStringProperty(this, "name", this.name);
		}
		return nameProperty;
	}

	public void setName(String name) {
		if(nameProperty ==null){
			this.name = name;
		}else{
			this.nameProperty.set(name);
		}
	}


}